﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace WindStyle.WindowsLiveWriter.Plugin.ExifInfo
{
    public static class APIHelper
    {
        static readonly Assembly dll_postEdior, dll_blogClient;
        
        static APIHelper()
        {
            dll_postEdior = Assembly.LoadFile(Path.Combine(Application.StartupPath, "WindowsLive.Writer.PostEditor.dll"));
            dll_blogClient = Assembly.LoadFile(Path.Combine(Application.StartupPath, "WindowsLive.Writer.BlogClient.dll"));
        }

        public static string DefaultBlogId
        {
            get
            {
                Type blogSettings = dll_blogClient.GetType("WindowsLive.Writer.BlogClient.BlogSettings");
                return blogSettings.GetProperty("DefaultBlogId").GetValue(null, null).ToString();
            }
        }

        internal static Size ScaleImageSizeName(string sizeName, string imgPath)
        {
            Type sizeNameEnum = dll_postEdior.GetType("WindowsLive.Writer.PostEditor.PostHtmlEditing.ImageSizeName");
            Type sizeUtil = dll_postEdior.GetType("WindowsLive.Writer.PostEditor.PostHtmlEditing.ImageUtils");
            var getImageSize = sizeUtil.GetMethod("GetImageSize");
            var scaleImageSizeName = sizeUtil.GetMethod("ScaleImageSizeName");
            var eSizeName = Enum.Parse(sizeNameEnum, sizeName);
            return (Size)scaleImageSizeName.Invoke(null, new object[]
                    { 
                        eSizeName, 
                        getImageSize.Invoke(null, new object[] { imgPath }),
                        RotateFlipType.RotateNoneFlipNone
                    });
        }

        internal static string GetEditorTemplateHtml()
        {
            Type postHtmlEditingSettings = dll_postEdior.GetType("WindowsLive.Writer.PostEditor.PostHtmlEditing.PostHtmlEditingSettings");
            var getEditorTemplateHtml = postHtmlEditingSettings.GetMethod("GetEditorTemplateHtml");
            var editingSettings = postHtmlEditingSettings.GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { DefaultBlogId });

            return getEditorTemplateHtml.Invoke(editingSettings, new object[] { 0, false }).ToString();
        }
    }
}
